package com.link_with_health.controller;

import com.link_with_health.common.Result;
import com.link_with_health.common.TokenMap;
import com.link_with_health.model.TalkRoomEntity;
import com.link_with_health.service.TalkRoomService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

@Controller
public class TalkRoomController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final String AUTH_TOKEN = "Authorization";
    private TokenMap tokenMap = TokenMap.getInstance();

    @Resource(name = "talkRoomService")
    TalkRoomService talkRoomService;




    @RequestMapping(value ="/talkRoom")
    public String talkRoomTest(){
        return "/page/talkRoom";
    }

    @RequestMapping(value ="/page/index")
    public String talkRoomIndex(){
        return "/page/index";
    }

    @GetMapping("/api/talkRoomInfo/selectByInquiry")
    @ResponseBody
    public Result selectByInquiry(@RequestParam("inquiryId")Long inquiry
            , HttpServletRequest request){
        String msg = "操作失败 验签失败";
        String token = request.getHeader(AUTH_TOKEN);
        if(token == null){
            logger.error("[request missing token]");
            return Result.unAuth(msg,"");
        }

        try{
            List<TalkRoomEntity> entity = talkRoomService
                    .getTalkRoomByInquiry(inquiry);
        } catch(Exception e){
            logger.error("[get talkRoom info failed]",e);
        }
        return Result.fail("获取聊天信息失败");
    }

    @GetMapping("/api/talkRoomInfo/selectById")
    @ResponseBody
    public Result selectById(@RequestParam("talkRoomId")Long id
            , HttpServletRequest request){
        String msg = "操作失败 缺少必请求要信息";
        String token = request.getHeader(AUTH_TOKEN);
        if(token == null){
            logger.error("[request missing token]");
            return Result.unAuth(msg,"");
        }

        try{
            TalkRoomEntity entity = talkRoomService
                    .getTalkRoomById(id);

            return Result.success(entity);

        } catch(Exception e){
            logger.error("[get talkRoom info failed]",e);
            msg = "获取聊天信息失败";
        }
        return Result.fail(msg);
    }


    @GetMapping("/api/accessTalkRoomUrl")
    public Result accessTalkRoomUrl(HttpServletRequest request){
        String msg = "操作失败 缺少必请求要信息";
        String token = request.getHeader(AUTH_TOKEN);
        if(token == null){
            logger.error("[request missing token]");
            return Result.unAuth(msg,"");
        }
        try {
            String url= "";
            Map<String,String> params = new HashMap<>();
        } catch(Exception e){
            logger.error("[request]");
            msg = "获取诊室信息错误信息";
        }

        return Result.fail(msg);
    }
}
